﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;

namespace Lost{
	///背包中某种物品的信息
	[System.Serializable]
	public class ItemInf
	{
		public ToolItem toolItem;
		public int num;
		public GameObject Prefab{
			get{ return toolItem.Prefab;}
		}
		public ItemInf(ToolItem it, int setNum=1){
			toolItem = it;
			num = setNum;
		}
	}

	/// <summary>
	/// 迷失背包
	/// </summary>
	[System.Serializable]
	public class LostBackpack {
		///所获得的物品列表
		public List<ItemInf> ItemList{
			get{ return itemList;}
		}
		[SerializeField] List<ItemInf> itemList = new List<ItemInf>();

		///第index种物品
		public ItemInf this[int index]{
			get{ return ItemList [index];}
		}
		///已拥有物品种数
		public int Count{
			get{ return ItemList.Count;}
		}

		///查找ItemInf
		public ItemInf FindItemInf(string itemName){
			foreach (ItemInf ii in itemList) {
				if (ii.toolItem.ItemName == itemName) {
					return ii;
				}
			}
			return null;
		}
		///是否存在某物品
		public bool Contains(string itemName){
			return (FindItemInf (itemName) != null);
		}
		///查找ItemInf的索引
		public int FindItemIndex(string itemName){
			int len = Count;
			for (int i = 0; i < len; i++) {
				if (itemList [i].toolItem.ItemName == itemName) {
					return i;
				}
			}
			return -1;
		}
		///添加物品
		public ItemInf Add(ToolItem item){
			ItemInf ii = FindItemInf (item.ItemName);
			if (ii != null) {
				ii.num++;
			} else {
				ii = new ItemInf (item);
				itemList.Add (ii);
			}
			return ii;
		}
		///从背包中删除物品
		public void Remove(string itemName){
			int index = FindItemIndex (itemName);
			if (index == -1) {
				Debug.Log ("There is no item named " + itemName + " in backpack!");
			}
			itemList.RemoveAt (index);
		}
		///从背包中删除物品
		public void RemoveAt(int index){
			if (index >= 0 && index < Count) {
				itemList.RemoveAt (index);
			}
		}
	}
}
